package cn.wolfcode.car.business.service.impl;


import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.CarPackageAudit;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.mapper.BpmnInfoMapper;
import cn.wolfcode.car.business.mapper.CarPackageAuditMapper;
import cn.wolfcode.car.business.mapper.ServiceItemMapper;
import cn.wolfcode.car.business.query.CarPackageAuditQuery;
import cn.wolfcode.car.business.service.ICarPackageAuditService;
import cn.wolfcode.car.common.base.page.TablePageInfo;

import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.shiro.ShiroUtils;
import com.github.pagehelper.PageHelper;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.activiti.image.impl.DefaultProcessDiagramGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.*;


/**
 * 套餐审核
 */
@Service
@Transactional
public class CarPackageAuditServiceImpl implements ICarPackageAuditService {

    @Autowired
    private CarPackageAuditMapper carPackageAuditMapper;
    @Autowired
    private BpmnInfoMapper bpmnInfoMapper;
    @Autowired
    private ServiceItemMapper serviceItemMapper;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private TaskService taskService;

    
    // 查询
    @Override
    public TablePageInfo<CarPackageAudit> query(CarPackageAuditQuery qo) {
        PageHelper.startPage(qo.getPageNum(), qo.getPageSize());
        return new TablePageInfo<CarPackageAudit>(carPackageAuditMapper.selectForList(qo));
    }


    // 进度查看
    @Override
    public InputStream processImg(Long id) {
        // 1.根据id 查询数据
        CarPackageAudit carPackageAudit = carPackageAuditMapper.selectByPrimaryKey(id);

        // 2.判断是否是审核中状态
        List<String> activitiHigthList = new ArrayList<>();
        if (carPackageAudit.getStatus().equals(CarPackageAudit.STATUS_IN_ROGRESS)){
            activitiHigthList = runtimeService.getActiveActivityIds(carPackageAudit.getInstanceId());
        }else {
            activitiHigthList = Collections.emptyList();
        }
        BpmnInfo bpmnInfo = bpmnInfoMapper.selectByPrimaryKey(carPackageAudit.getBpmnInfoId());

        /** 响应png
         * 第一个参数: BpmnModel  第二个参数: 高亮节点集合   第三个参数: 高亮连线集合
         */
        DefaultProcessDiagramGenerator generator = new DefaultProcessDiagramGenerator();
        InputStream inputStream = generator.generateDiagram(repositoryService.getBpmnModel(bpmnInfo.getActProcessId()),
                activitiHigthList,
                Collections.emptyList(), "宋体", "宋体", "宋体");

        return inputStream;
    }


    // 撤销
    @Override
    @Transactional
    public void cancelApply(Long id) {
        //1. 根据id查询数据
        CarPackageAudit carPackageAudit = carPackageAuditMapper.selectByPrimaryKey(id);
        //2. 判断状态必须为审核中状态,不是审核中就不能撤销,否则抛异常
        if (!CarPackageAudit.STATUS_IN_ROGRESS.equals(carPackageAudit.getStatus())){
            throw new BusinessException("非法操作,不是审核中就不能撤销");
        }

        //3. 是审核中就修改carPackageAudit 状态 为撤销状态
        carPackageAuditMapper.changeStatus(id,CarPackageAudit.STATUS_CANCEL);
        //4. 修改单项状态为 初始化状态
        serviceItemMapper.changAuditStatus(carPackageAudit.getServiceItemId(), ServiceItem.AUDITSTATUS_INIT);
        //5. 把流程实例给删除掉
        runtimeService.deleteProcessInstance(carPackageAudit.getInstanceId(),"删除");
    }


    //审批确定
    @Override
    @Transactional
    public void audit(Long id, Integer auditStatus, String info) {
        // 根据id 把数据查询出来
        CarPackageAudit carPackageAudit = carPackageAuditMapper.selectByPrimaryKey(id);
        // 判断状态要为审核中,才可以审批
        if (!CarPackageAudit.STATUS_IN_ROGRESS.equals(carPackageAudit.getStatus())){
            throw new BusinessException("非法操作!审核中才可以审批");
        }

        // 查询当前任务节点
        Task task = taskService.createTaskQuery().processInstanceId(carPackageAudit.getInstanceId()).singleResult();
        // 完成任务,在完成任务时候要设置流程变量
        HashMap<String, Object> map = new HashMap<>();
        map.put("auditStatus",auditStatus); // 往map存审核状态
        taskService.complete(task.getId(),map); //执行任务

        // 判断是审核通过还是审核拒绝
        if (CarPackageAudit.STATUS_PASS.equals(auditStatus)){ //审核通过
            //获取当前任务节点
            Task nextTask = taskService.createTaskQuery().processInstanceId(carPackageAudit.getInstanceId()).singleResult();
            if (nextTask != null){ //如果下个任务节点不是空
                // 说明有下一个节点, 把下一个节点审核人设置到 carPackageAudit 当中
                carPackageAudit.setAuditorId(Long.parseLong(nextTask.getAssignee())); //Long.parseLong 转换为Long类型(本来是String类型的)
            }else {
                // 修改单项状态为审核通过
                serviceItemMapper.changAuditStatus(carPackageAudit.getServiceItemId(),ServiceItem.AUDITSTATUS_APPROVED);
                // 修改carPackageAudit 状态为审核同意状态
                carPackageAudit.setStatus(CarPackageAudit.STATUS_PASS);
            }
            carPackageAudit.setInfo(carPackageAudit.getInfo() + "--->审核人为:[" + ShiroUtils.getUser().getUserName() + "]-同意-备注:" + info);

        } else if(CarPackageAudit.STATUS_REJECT.equals(auditStatus)){ //审核是拒绝
            // 修改单项状态为重新调整
            serviceItemMapper.changAuditStatus(carPackageAudit.getServiceItemId(),ServiceItem.AUDITSTATUS_REPLY);
            // 修改carPackageAudit 状态为审核拒绝状态
            carPackageAudit.setStatus(CarPackageAudit.STATUS_REJECT);

            carPackageAudit.setInfo(carPackageAudit.getInfo() + "--->审核人为:[" + ShiroUtils.getUser().getUserName() + "]-拒绝-备注:" + info);
        }

        carPackageAudit.setAuditTime(new Date());
        carPackageAuditMapper.updateByPrimaryKey(carPackageAudit);

    }


}












